﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class OptProductProjectsAdd : System.Web.UI.Page
{
    LogDB m_logdb = new LogDB();
    ProductDB m_productdb = new ProductDB();


    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }


    private void GetProjectList(string projectCode)
    {
        DataTable dt;
        dt = m_productdb.getProjectListByPjCode(projectCode); ;
        gvProductProject.DataSource = dt;
        gvProductProject.DataBind();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }

        if (!this.Page.IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Products.ToString() + " | " + ISitemap.ProductProjectsAdd.ToString();
            string model;
            string modelName;
            model = this.Request["Model"].ToString();
            modelName = this.Request["ModelName"].ToString();

            this.txtModel.Text = model;
            this.txtModelName.Text = modelName;
            GetProjectList("");
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string pjCode;
        int ret;
        string strSuc = "";
        string strFaile = "";

        for (int i = 0; i < gvProductProject.Rows.Count; i++)
        {
            //搜索第n行3列 
            CheckBox cbx = (CheckBox)gvProductProject.Rows[i].FindControl("cbxId");
            if (cbx.Checked == true)
            {
                pjCode = gvProductProject.DataKeys[i].Values["ProjectCode"].ToString();
                ret = m_productdb.insertProjectProject(this.txtModel.Text.Trim(), pjCode);
                if (ret == 0)
                {
                    strSuc += pjCode + ",";
                }
                else
                {
                    strFaile += pjCode + ",";
                }
            }
        }
        if (strFaile == "")
        {
            Jscript.Alert("Add all project to product succeed!", this.Page);
        }
        else if (strSuc == "")
        {
            Jscript.Alert("Add all project to product faile!", this.Page);
        }
        else
        {
            Jscript.Alert("Add all project to product (" + strSuc + ") is succeed (" + strFaile + ") is faile!", this.Page);
        }
        GetProjectList("");
    }

    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetProjectList(this.txtProjectCode.Text.Trim());
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("OptProductProjects.aspx?Model=" + this.txtModel.Text + "&ModelName=" + this.txtModelName.Text);
    }


    protected void gvProductProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProductProject.PageIndex = e.NewPageIndex;
        GetProjectList(this.txtProjectCode.Text.Trim());
    }
}
